package com.zz.serialize;

import java.io.*;

public class JavaSerializer implements Iserializer {
    @SuppressWarnings("unchcked")
    public <T> byte[] serialize(T obj) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream  out = new ObjectOutputStream(outputStream);
            out.writeObject(obj);
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
         throw new RuntimeException(e);
        }
        return outputStream.toByteArray();
    }
    @SuppressWarnings("unchcked")
    public <T> T dserialize(byte[] data, Class<T> clazz) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
        try {
            ObjectInputStream input = new ObjectInputStream(inputStream);
            return (T)input.readObject();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
